#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
//0-9数字键输入
void MainWindow::on_pushButton_0_clicked()
{
    list.append("0");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_1_clicked()
{
    list.append("1");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_2_clicked()
{
    list.append("2");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_3_clicked()
{
    list.append("3");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_4_clicked()
{
    list.append("4");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_5_clicked()
{
    list.append("5");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_6_clicked()
{
    list.append("6");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_7_clicked()
{
    list.append("7");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_8_clicked()
{
    list.append("8");
    this->ui->lineEdit->setText(list);

}

void MainWindow::on_pushButton_9_clicked()
{
    list.append("9");
    this->ui->lineEdit->setText(list);

}
//小数点
void MainWindow::on_pushButton_point_clicked()
{
    list.append(".");
    this->ui->lineEdit->setText(list);

}
//加法
void MainWindow::on_pushButton_plus_clicked()
{
    list.append(" + ");
    this->ui->lineEdit->setText(list);
    symbol = '+';

}
//减法
void MainWindow::on_pushButton_minus_clicked()
{
    list.append(" - ");
    this->ui->lineEdit->setText(list);
    symbol = '-';

}
//乘法
void MainWindow::on_pushButton_multiply_clicked()
{
    list.append(" * ");
    this->ui->lineEdit->setText(list);
    symbol = '*';

}
//除法
void MainWindow::on_pushButton_divide_clicked()
{
    list.append(" / ");
    this->ui->lineEdit->setText(list);
    symbol = '/';

}
//删除输入框最后一位
void MainWindow::on_pushButton_del_clicked()
{
    list.chop(1);
    this->ui->lineEdit->setText(list);

}
//清除输入框结果框内容
void MainWindow::on_pushButton_c_clicked()
{
    list.clear();
    this->ui->lineEdit->clear();
    this->ui->lineEdit_2->clear();

}
//储存当前结果
void MainWindow::on_pushButton_ms_clicked()
{
    ms = ans;
}
//释放储存的值
void MainWindow::on_pushButton_mr_clicked()
{
    QString mr = QString::number(ms);
    list.append(mr);
    this->ui->lineEdit->setText(list);
}
//按下等于号后计算输入框中表达式的结果
void MainWindow::on_pushButton_equal_clicked()
{
    this->ui->lineEdit_2->clear();
    if(symbol == ' '){
        this->ui->lineEdit_2->setText(list);
        list.clear();
        return;
    }
    QStringList numlist = list.split(" ");//将输入框中内容分解为三部分，左右两部分的数字及中间的运算符
    bool ok;
    double num_1 = numlist[0].toDouble(&ok);
    if(!ok){
        this->ui->lineEdit_2->setText("Error Input!");
        list.clear();
        return;
    }
    double num_2 = numlist[2].toDouble(&ok);
    if(!ok){
        this->ui->lineEdit_2->setText("Error Input!");
        list.clear();
        return;
    }
    switch(symbol)//判断当前运算符并进行运算
    {
        case('+'):
            ans = num_1 + num_2;
        break;

        case('-'):
            ans = num_1 - num_2;
        break;

        case('*'):
            ans = num_1 * num_2;
        break;

        case('/'):
        if(num_2 == 0.0){
            this->ui->lineEdit_2->setText("Error!");
            list.clear();
            return;
        }
        else
            ans = num_1 / num_2;
        break;
        default: ans = num_1;

    }
    symbol = ' ';
    QString str = QString::number(ans);
    this->ui->lineEdit_2->setText(str);
    list.clear();
}
